package cn.stylefeng.guns.modular.gridsystem.temperature.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.context.login.LoginContextHolder;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.modular.gridsystem.temperature.entity.BizTemperatureException;
import cn.stylefeng.guns.modular.gridsystem.temperature.entity.BizTemperatureExceptionLog;
import cn.stylefeng.guns.modular.gridsystem.temperature.entity.BizTemperatureRecord;
import cn.stylefeng.guns.modular.gridsystem.temperature.mapper.BizTemperatureExceptionLogMapper;
import cn.stylefeng.guns.modular.gridsystem.temperature.mapper.BizTemperatureExceptionMapper;
import cn.stylefeng.guns.modular.gridsystem.temperature.mapper.BizTemperatureRecordMapper;
import cn.stylefeng.guns.modular.gridsystem.temperature.model.param.BizTemperatureExceptionParam;
import cn.stylefeng.guns.modular.gridsystem.temperature.model.result.BizTemperatureExceptionLogResult;
import cn.stylefeng.guns.modular.gridsystem.temperature.model.result.BizTemperatureExceptionResult;
import cn.stylefeng.guns.modular.gridsystem.temperature.model.result.BizTemperatureRecordResult;
import cn.stylefeng.guns.modular.gridsystem.temperature.service.BizTemperatureExceptionLogService;
import cn.stylefeng.guns.modular.gridsystem.temperature.service.BizTemperatureExceptionService;
import cn.stylefeng.guns.modular.gridsystem.temperature.service.BizTemperatureRecordService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class BizTemperatureExceptionServiceImpl extends ServiceImpl<BizTemperatureExceptionMapper, BizTemperatureException> implements BizTemperatureExceptionService {
    @Autowired
    private BizTemperatureExceptionLogMapper temperatureExceptionLogMapper;


    @Override
    public PageResult<BizTemperatureExceptionResult> pageList(BizTemperatureExceptionParam param) {
        Page<BizTemperatureExceptionResult> page = this.baseMapper.page(PageFactory.defaultPage(), param);
        return new PageResult<>(page);
    }

    @Override
    public void del(BizTemperatureExceptionParam param) {
        BizTemperatureException bizTemperatureException = this.getById(param.getId());
        if (ObjectUtil.isEmpty(bizTemperatureException)) {
            throw new ServiceException("操作的内容不存在！");
        }
        LambdaUpdateWrapper<BizTemperatureExceptionLog> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(BizTemperatureExceptionLog::getExceptionId, param.getId());
        temperatureExceptionLogMapper.delete(updateWrapper);
        this.updateById(bizTemperatureException);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void changeStatus(BizTemperatureExceptionParam param) {
        if (param.getStatus() ==null) {
            throw new ServiceException("处理状态不能为空！");
        }
        if (param.getId() ==null) {
            throw new ServiceException("信息无法绑定，请查询当前是否拥有异常信息！");
        }
        Long sysLoginUserOrgId = LoginContextHolder.me().getSysLoginUserOrgId();
        BizTemperatureException bizTemperatureException = this.getById(param.getId());
        if (ObjectUtil.isEmpty(bizTemperatureException)) {
            throw new ServiceException("操作的内容不存在！");
        }
        bizTemperatureException.setStatus(param.getStatus());
        bizTemperatureException.setManageUserId(sysLoginUserOrgId);
        bizTemperatureException.setManageTime(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));

        //该任务已被处理
        BizTemperatureExceptionLog bizTemperatureExceptionLog = new BizTemperatureExceptionLog();
        bizTemperatureExceptionLog.setExceptionId(param.getId());
        bizTemperatureExceptionLog.setManageUserId(bizTemperatureException.getManageUserId());
        bizTemperatureExceptionLog.setManageTime(bizTemperatureException.getManageTime());
        bizTemperatureExceptionLog.setStatus(bizTemperatureException.getStatus());
        temperatureExceptionLogMapper.updateByExceptionId(bizTemperatureExceptionLog);
        this.updateById(bizTemperatureException);
    }
}
